﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SpaceInvaderz
{
    public partial class Page : UserControl
    {

        public GameCore gameCore = null;
        public Double gameTime;
        public Double lastTime;
        public Point WorldLimits;

        public Page()
        {
            InitializeComponent();
            App.Current.Host.Content.Resized += new EventHandler(Content_Resized);

        }

        public void Initialize()
        {

            WorldLimits.Y = Application.Current.Host.Content.ActualHeight;
            WorldLimits.X = Application.Current.Host.Content.ActualWidth;

            gameCore = new GameCore(this);
            gameTime = DateTime.Now.TimeOfDay.TotalMilliseconds / 1000;

        }

        private void StartTimer(object sender, RoutedEventArgs e)
        {
            System.Windows.Threading.DispatcherTimer myDispatcherTimer =
                new System.Windows.Threading.DispatcherTimer();
            myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
            myDispatcherTimer.Tick += new EventHandler(Each_Tick);
            myDispatcherTimer.Start();
            //ImageBrush Image1 = new ImageBrush();


            //StreamResourceInfo sr1 = Application.GetResourceStream(new Uri(@"SilverlightAnimatedTimer01;component/Images/BlueLight.png", UriKind.Relative));


            //BitmapImage bmp1 = new BitmapImage();

            //bmp1.SetSource(sr1.Stream);

            //Image1.ImageSource = bmp1;
            //Shape shape = new Rectangle();
            //shape.Fill = Image1;
            //shape.Visibility = Visibility.Visible;
            //shape.Width = 100;
            //shape.Height = 100;
            //myCanvas.Children.Add(shape);




            //ImageBrush brush = new ImageBrush();
            //BitmapImage bmpImg2 = new BitmapImage();
            //Uri uri2 = new Uri(@"Images\Enemy.png", UriKind.Relative);
            //bmpImg2.UriSource = uri2;
            //brush.ImageSource = bmpImg2;
            //Shape shape = new Rectangle();
            //shape.Stroke = brush;
            //myCanvas.Children.Add(shape);

        }


        void Goober()
        {
        }

        public void Each_Tick(object o, EventArgs sender)
        {
            lastTime = gameTime;
            gameTime = DateTime.Now.TimeOfDay.TotalMilliseconds / 1000;
            gameCore.MainLoop(gameTime, lastTime);
        }



        private void Canvas_KeyDown(object sender, KeyEventArgs e)
        {

            double speed = gameCore.Speed;
            switch (e.Key)
            {

                case (Key.Space):
                    if (gameCore.Points > 20)
                    {
                        gameCore._enviroment.Shoot();
                        Laser.Stop();
                        Laser.Play();
                        gameCore.Points -= 20;
                    }

                    break;
                case (Key.Up):
                    gameCore._hero.VelocityY = speed;
                    break;

                case (Key.Down):
                    gameCore._hero.VelocityY = -speed;
                    break;

                case (Key.Left):
                    gameCore._hero.VelocityX = -speed * 1.5;
                    break;

                case (Key.Right):
                    gameCore._hero.VelocityX = speed / 1.5;
                    break;


                default:
                    break;
            }
        }

        private void UserControl_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case (Key.Up):
                    gameCore._hero.VelocityY = 0;
                    break;

                case (Key.Down):
                    gameCore._hero.VelocityY = 0;
                    break;

                case (Key.Left):
                    gameCore._hero.VelocityX = 0;
                    break;

                case (Key.Right):
                    gameCore._hero.VelocityX = 0;
                    break;


                default:
                    break;
            }
        }

        public void Content_Resized(object o, EventArgs sender)
        {
            WorldLimits.Y = Application.Current.Host.Content.ActualHeight;
            WorldLimits.X = Application.Current.Host.Content.ActualWidth;
            Initialize();
        }

    }
}
